<?php
/**
 * Joomla! 1.5 component JoomGPSTracks
 *
 * @version $Id: cats.php 2009-06-24 15:40:19 svn $
 * @author Michael Pfister
 * @package joomGPStracks
 * @subpackage backend
 * @license GNU/GPL
 * @filesource
 *
 * 
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport('joomla.application.component.model');
/**
 * Model Class Categories
 */
class JoomgpstracksModelCats extends JModel {

	/**
	 * Category ata array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * Category total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;
    /**
     *
     * @global object $mainframe
     * @global string $option
     */
    function __construct() {
            parent::__construct();
            global $mainframe, $option;

            // Get the pagination request variables
            $limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
            $limitstart	= $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );

            // In case limit has been changed, adjust limitstart accordingly
//		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
            $limitstart = JRequest::getVar('limitstart',0);

            $this->setState('limit', $limit);
            $this->setState('limitstart', $limitstart);
    }

        /**
         *
         * @return object
         */
	function getData()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}

		return $this->_data;
	}
    /**
     *
     * @return array $pagination
     */
 	function getPagination()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}

    /**
     *
     * @return int
     */
    function getTotal()  {

		// Lets load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
    }

    /**
     *
     * @global object $mainframe
     * @return string
     */

    function _buildQuery()  {
        global $mainframe;

        $db =& JFactory::getDBO();

        $query = "SELECT * FROM #__gps_cats ORDER BY ordering"
                ;

        return $query;
    }

    /**
     *
     * @global object $mainframe
     * @param int $id
     * @return object
     */
    function getCat($id)  {
        global $mainframe;

        $db =& JFactory::getDBO();

        $query = "SELECT * FROM #__gps_cats"
                . "\n WHERE id='" . $id . "'";

        $db->setQuery($query);
        $result = $db->loadObject();

        if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}

        return $result;
    }


    function getParent()  {
        global $mainframe;

        $db = JFactory::getDBO();

        $query = "SELECT id, title FROM #__gps_cats"
                . "\n WHERE parent=0"
                . "\n ORDER BY title ASC"
                ;

        $db->setQuery($query);
        $result = $db->loadObjectList();

        return $result;
    }

    /**
     *
     * @param array $rows
     * @return string
     */
    function createCatlist($rows)  {
        $children = array();
	foreach ($rows as $v ) {
		$pt 	= $v->parent;
		$list 	= @$children[$pt] ? $children[$pt] : array();
		array_push( $list, $v );
		$children[$pt] = $list;
	}
        $levellimit = 50;
        $list = JHTML::_('menu.treerecurse', 0, '', array(), $children, max( 0, $levellimit-1 ) );
        $list = array_slice($list, $this->getState('limitstart'), $this->getState('limit'));

        return $list;
    }
}
?>
